﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace ChangeLanguage
{
    public class ResourceService : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private static readonly ResourceService _current = new ResourceService();
        public static ResourceService Current => _current;

        readonly Properties.Resources resource = new Properties.Resources();
        public Properties.Resources Resources => resource;

        protected virtual void RaisePropertyChanged(string propertyName = null)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        /// <summary>
        /// 语言列表
        /// </summary>
        public static List<ValueText> Languages = new List<ValueText>() {
                new ValueText("","中文"),
                new ValueText("en-US","English")
        };
        /// <summary>
        /// 变更
        /// </summary>
        public void ChangedCulture(string name)
        {
            //变更配置文件默认语言
            Properties.Settings.Default.Language = name;
            Properties.Settings.Default.Save();
            //获取变更的值
            var currLanguage = Languages.FirstOrDefault(t => t.Text == name).Value;
            if (currLanguage == null) {
                currLanguage = Languages.FirstOrDefault().Value;
            }
            Properties.Resources.Culture = CultureInfo.GetCultureInfo(currLanguage);//变更语言
            this.RaisePropertyChanged("Resources");//通知UI更新
        }
    }
    public class ValueText
    {
        public ValueText()
        {

        }
        public ValueText(string _Value, string _Text)
        {
            Value = _Value;
            Text = _Text;
        }
        public string Value { get; set; }
        public string Text { get; set; }
    }

}
